<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>忘记密码</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="layui/css/layui.css" media="all">
    <link rel="stylesheet" href="layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" href="layuiadmin/style/login.css" media="all">
</head>
<body>
<div class="layadmin-user-login layadmin-user-display-show" id="LAY-user-login" style="padding:0">
    <div class="layadmin-user-login-main">
        <div class="layadmin-user-login-box layadmin-user-login-header">
            <h2>Orange</h2>
            <p>忘记密码</p>
        </div>
        <div class="layadmin-user-login-box layadmin-user-login-body layui-form">

            <script type="text/html" template>
                {{# if(layui.router().search.type === 'resetPass'){ }}
                <div class="layui-form-item">
                    <label class="layadmin-user-login-icon layui-icon layui-icon-password"
                           for="LAY-user-login-password"></label>
                    <input type="password" name="password" id="LAY-user-login-password" lay-verify="pass"
                           placeholder="新密码" class="layui-input" autocomplete="off">
                </div>
                <div class="layui-form-item">
                    <label class="layadmin-user-login-icon layui-icon layui-icon-password"
                           for="LAY-user-login-repass"></label>
                    <input type="password" name="repass" id="LAY-user-login-repass" lay-verify="required"
                           placeholder="确认密码" class="layui-input" autocomplete="off">
                </div>
                <div class="layui-form-item">
                    <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="user-forget-resetpass">重置新密码
                    </button>
                </div>
                {{# } else { }}
                <div class="layui-form-item">
                    <label class="layadmin-user-login-icon layui-icon layui-icon-username"></label>
                    <input type="text" name="account" id="userid" lay-verify="required"
                           placeholder="用户编号" class="layui-input">
                </div>
                <div class="layui-form-item">
                    <label class="layadmin-user-login-icon layui-icon layui-icon-website"></label>
                    <input type="text" name="useremail" id="useremail" lay-verify="email" placeholder="请输入注册时填写的邮箱"
                           class="layui-input">
                </div>
                <div class="layui-form-item">
                    <div class="layui-row">
                        <div class="layui-row">
                            <div class="layui-col-xs7">
                                <label class="layadmin-user-login-icon layui-icon layui-icon-vercode"></label>
                                <input type="text" name="code" id="login-vercode" lay-verify="required"
                                       placeholder="图形验证码" class="layui-input">
                            </div>
                            <div class="layui-col-xs5">
                                <div style="margin-left: 5px;">
                                    <img th:src="@{/captcha}" id="code">
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-row">
                            <div class="layui-col-xs7">
                                <label class="layadmin-user-login-icon layui-icon layui-icon-vercode"
                                       for="LAY-user-login-smscode"></label>
                                <input type="text" name="emailcode" id="LAY-user-login-smscode" lay-verify="required"
                                       placeholder="邮箱验证码" class="layui-input">
                            </div>
                            <div class="layui-col-xs5">
                                <div style="margin-left: 10px;">
                                    <input id="get-code" class="layui-btn layui-btn-primary layui-btn-fluid"
                                           name="send-email-code" type="button" value="获取验证码"/>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="LAY-user-forget-submit">找回密码
                        </button>
                    </div>
                    <div class="layui-trans layui-form-item layadmin-user-login-other">
                        <a th:href="@{/login}" class="layadmin-user-jump-change layadmin-link "><label>用户登入</label></a>
                    </div>
                    {{# } }}
                </div>    </script>

        </div>
    </div>
</div>
</body>
<script src="layui/layui.js"></script>
<script>
    layui.config({
        base: 'layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'user'], function () {
        var $ = layui.$
            , setter = layui.setter
            , admin = layui.admin
            , form = layui.form
            , router = layui.router();

        form.render();

        //更换验证码
        $(function () {
            $("#code").click(function () {
                $(this).attr("src", "/captcha?v=" + Math.round(Math.random()*10));
            });
        });

        var email;  //邮箱
        //验证用户是否存在
        $("#userid").blur(function () {
            var id = $("#userid").val();
            if (id == "") {
                layer.msg("请输入用户编号", {icon: 5, time: 1000})
            } else {
                $.ajax({
                    url: "/users/",
                    type: "get",
                    data: {
                        account: id
                    },
                    dataType: "json",
                    success: function (data) {
                        if (data.status == 200) {
                            layer.msg("用户编号不存在！", {icon: 5, time: 1000});
                            $("#userid").focus();
                            $("#userid").val("");
                            return false;
                        } else {
                            email = data.email;
                        }
                    }
                });
            }
        });
        var emailcode;     //邮箱验证码
        var InterValObj; //timer变量，控制时间
        var count = 60; //间隔函数，1秒执行
        var curCount;//当前剩余秒数
        //发送邮箱验证码
        $(function () {
            $("#get-code").click(function () {
                var inemail = $("#useremail").val();
                var id = $("#userid").val();
                var verify = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
                if (!verify.test(inemail)) {
                    layer.msg('邮箱格式不正确！请重新输入', {icon: 5, shade: [0.5, '#000000'], shadeClose: true});
                    return false;
                } else if (email != inemail) {
                    layer.msg('邮箱错误，请联系管理员！', {icon: 5, shade: [0.5, '#000000'], shadeClose: true});
                    return false;
                } else {
                    //请求发送验证码
                    $.ajax({
                        type: "POST",  //提交方式
                        dataType: 'json',
                        url: "${pageContext.request.contextPath}/manage/getmailcode.do",//路径
                        data: {useremail: email, usercode: id},//数据，这里使用的是Json格式进行传输
                        success: function (data) {//返回数据根据结果进行相应的处理
                            if (data.msg == "1" && data.code != null) {
                                // 设置button效果，开始计时
                                curCount = count;
                                document.getElementById("get-code").setAttribute("disabled", "true");//设置按钮为禁用状态
                                document.getElementById("get-code").value = curCount + "秒后重获";//更改按钮文字
                                InterValObj = window.setInterval(SetRemainTime, 1000); // 启动计时器timer处理函数，1秒执行一次
                                emailcode = data.code;
                            } else {
                                layer.msg('验证码获取失败，请重新获取', {icon: 5, shade: [0.5, '#000000'], shadeClose: true});
                                return false;
                            }
                        }
                    });
                }
            });
        });

        //timer处理函数
        function SetRemainTime() {
            if (curCount == 0) {//超时重新获取验证码
                emailcode = "";
                window.clearInterval(InterValObj);// 停止计时器
                document.getElementById("get-code").removeAttribute("disabled");//移除禁用状态改为可用
                document.getElementById("get-code").value = "重获验证码";
            } else if (curCount == 20) {
                layer.msg('没有收到？请查看垃圾箱', {icon: 6, time: 1000});
                curCount--;
            } else {
                curCount--;
                document.getElementById("get-code").value = curCount + "秒后重获";
            }
        };

        //验证验证码
        $("#LAY-user-login-smscode").blur(function () {
            var vercode = $("#LAY-user-login-smscode").val();
            if (vercode != emailcode) {
                layer.msg('验证码错误', {icon: 5, time: 1000});
                return false;
            }
        });

        //找回密码下一步
        form.on('submit(LAY-user-forget-submit)', function () {
            location.hash = '/type=resetPass';
            location.reload();
        });

        //重置密码
        form.on('submit(user-forget-resetpass)', function (obj) {
            var field = obj.field;
            //确认密码
            if (field.password !== field.repass) {
                return layer.msg('两次密码输入不一致');
            }
            //请求接口
            $.post({
                url: '${pageContext.request.contextPath}/manage/getbackpwd.do' //实际使用请改成服务端真实接口
                , data: {
                    userpassword: field.password,
                    usercode: '${sessionScope.usercode}',
                    useremail: '${sessionScope.email}'
                }
                , success: function (data) {
                    if (data.msg == 1) {
                        layer.msg('密码已成功重置', {
                            offset: '15px'
                            , icon: 1
                            , time: 1000
                        }, function () {
                            location.href = '${pageContext.request.contextPath}/manage/login.do'; //跳转到登入页
                        });
                    }
                    else {
                        layer.msg('密码重置失败，请联系管理员', {
                            offset: '15px'
                            , icon: 2
                            , time: 1000
                        }, function () {
                            location.href = '${pageContext.request.contextPath}/manage/forget.do'; //跳转到登入页
                        });
                    }
                }
            });
            return false;
        });
    });
</script>
</html>